package com.blackbox.price.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.blackbox.price.configuration.VisionResult;
import com.blackbox.price.entity.BbChildUser;
import com.blackbox.price.entity.BbImg;
import com.blackbox.price.entity.BbUser;
import com.blackbox.price.service.ChildUserService;
import com.blackbox.price.service.ImgService;
import com.blackbox.price.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: TWQ
 * @Description:
 * @Date: Created in 下午 23:02 2020/8/13 0024
 * @Modidied By: NO
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "用户管理")
@RequestMapping("user")
public class UserLoginController {

    @Value("${fileUpload.path}")
    private String filePath;

    @Resource
    private UserService userService;

    @Resource
    private ChildUserService childUserService;

    @Resource
    private ImgService imgService;

    @PostMapping("/postUserList")
    @ResponseBody
    @ApiOperation(value = "查询用户列表")
    public VisionResult postUserList() {
        return VisionResult.oK(userService.queryUserList());
    }

    @PostMapping("/postShowUserList")
    @ResponseBody
    @ApiOperation(value = "查询显示价格用户列表")
    public VisionResult postShowUserList() {
        return VisionResult.oK(userService.queryShowUserList());
    }

    @PostMapping("/addUser")
    @ResponseBody
    @ApiOperation(value = "新增用户")
    public VisionResult addUser(BbUser user, String imgId, String[] child) {
        user.setImg(filePath+imgId);
        userService.addUser(user);
        if(child.length > 0) {
            this.childUserService.setPidToChildUser(user.getId(), child);
        }
        return VisionResult.oK();
    }


    @PostMapping("/updateUser")
    @ResponseBody
    @ApiOperation(value = "修改用户列表")
    public VisionResult updateUser(BbUser user, String imgId, String[] child) {
        if(!StringUtils.isEmpty(imgId)) {
            user.setImg(filePath+imgId);
        }
        //this.childUserService.deleteByPid(user.getId());
        if(child.length > 0) {
            this.childUserService.setPidToChildUser(user.getId(), child);
        }
        return VisionResult.oK(userService.update(user));
    }

    @PostMapping("/addChildUser")
    @ResponseBody
    @ApiOperation(value = "新增用户子项")
    public VisionResult addChildUser(BbChildUser childUser) {
        return VisionResult.oK(childUserService.addChildUser(childUser));
    }

    @PostMapping("/postUserById")
    @ResponseBody
    @ApiOperation(value = "根据Id查询用户")
    public VisionResult postUserById(Integer id) {
        return VisionResult.oK(userService.queryById(id));
    }

    @PostMapping("/postChildUserListById")
    @ResponseBody
    @ApiOperation(value = "根据Id查询用户子项")
    public VisionResult postChildUserListById(Integer id) {
        return VisionResult.oK(childUserService.queryChildListByPid(id));
    }

    @PostMapping("/deleteUserById")
    @ResponseBody
    @ApiOperation(value = "删除用户")
    public VisionResult deleteUserById(String id) {
        return VisionResult.oK(userService.deleteById(id));
    }

    @PostMapping("/deleteChildUserById")
    @ResponseBody
    @ApiOperation(value = "删除用户子项")
    public VisionResult deleteChildUserById(String id) {
        return VisionResult.oK(childUserService.deleteById(id));
    }
}
